package com.ruoyi.web.controller.basic;

import com.ruoyi.basic.domain.TbCategoryReal;
import com.ruoyi.basic.service.ITbCategoryRealService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类Controller
 * 
 * @author ruoyi
 * @date 2022-09-04
 */
@Controller
@RequestMapping("/system/real")
public class TbCategoryRealController extends BaseController
{
    private String prefix = "basic/categoryreal";

    @Autowired
    private ITbCategoryRealService TbCategoryRealService;

    @RequiresPermissions("system:real:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/categoryreal";
    }

    /**
     * 查询分类列表
     */
    @RequiresPermissions("system:real:list")
    @PostMapping("/list")
    @ResponseBody
    public List<TbCategoryReal>  list(TbCategoryReal TbCategoryReal)
    {
        List<TbCategoryReal> list = TbCategoryRealService.listByCondition(TbCategoryReal);
        return list;
    }

    /**
     * 查询分类列表
     */
    @RequiresPermissions("system:real:list")
    @PostMapping("/simple/list")
    @ResponseBody
    public TableDataInfo simpleList(TbCategoryReal TbCategoryReal)
    {
        startPage();
        List<TbCategoryReal> list = TbCategoryRealService.listByCondition(TbCategoryReal);
        return getDataTable(list);
    }

    /**
     * 新增分类
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId,ModelMap mmap)
    {
        mmap.put("category", TbCategoryRealService.getById(parentId));
        return prefix + "/add";
    }

    /**
     * 新增保存分类
     */
    @RequiresPermissions("system:real:add")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TbCategoryReal TbCategoryReal)
    {
        return toAjax(TbCategoryRealService.saveCategory(TbCategoryReal));
    }

    /**
     * 修改分类
     */
    @RequiresPermissions("system:real:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbCategoryReal category = TbCategoryRealService.getCategoryById(id);
        mmap.put("category", category);
        return prefix + "/edit";
    }

    /**
     * 修改保存分类
     */
    @RequiresPermissions("system:real:edit")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave( @Validated TbCategoryReal TbCategoryReal)
    {
        return toAjax(TbCategoryRealService.updateCategory(TbCategoryReal));
    }

    /**
     * 删除分类
     */
    @RequiresPermissions("system:real:remove")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        if (TbCategoryRealService.selectCategoryCount(id) > 0)
        {
            return AjaxResult.warn("存在下级分类,不允许删除");
        }
//        if (TbCategoryRealService.checkCategoryExistDailyUnitPrice(id))
//        {
//            return AjaxResult.warn("部门存在用户,不允许删除");
//        }
        return toAjax(TbCategoryRealService.removeById(id));
    }

    /**
     * 校验分类名称
     */
    @PostMapping("/checkCategoryNameUnique")
    @ResponseBody
    public String checkDeptNameUnique(TbCategoryReal category)
    {
        return TbCategoryRealService.checkCategoryNameUnique(category);
    }

    /**
     * 选择分类树
     *
     * @param categoryId 分类ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectCategoryTree/{categoryId}", "/selectCategoryTree/{categoryId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("categoryId") Long categoryId,
                                 @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap)
    {
        mmap.put("category", TbCategoryRealService.getById(categoryId));
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /**
     * 加载分类列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = TbCategoryRealService.selectCategoryTree(new TbCategoryReal());
        return ztrees;
    }

    /**
     * 加载分类列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId) {
        TbCategoryReal category = new TbCategoryReal();
        category.setExcludeId(excludeId);
        List<Ztree> ztrees = TbCategoryRealService.selectDeptTreeExcludeChild(category);
        return ztrees;
    }

}
